package com.cskaoyan.service;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.GoodsExample;
import com.cskaoyan.mapper.GoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService{

    @Autowired
    GoodsMapper goodsMapper;
    //GoodsExample example = new GoodsExample();
    //GoodsExample.Criteria criteria = example.createCriteria();//提取出来才是全局的条件

    @Override
    public BaseRespData query(String goodsSn, String name, BaseParam baseParam) {
        //分页
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        GoodsExample example = new GoodsExample();
        //排序
        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        //查询条件
        GoodsExample.Criteria criteria = example.createCriteria();//提取出来才是全局的条件
        if (goodsSn != null && !"".equals(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        //List<Goods> items = goodsMapper.selectByExample(example);
        List<Goods> items = goodsMapper.selectByExampleWithBLOBs(example);
        PageInfo<Goods> pageInfo = new PageInfo<>(items);
        long total = pageInfo.getTotal();

        return BaseRespData.create(items,total);
    }
}
